using System;
using System.Data;
using System.Data.SqlClient; 
//using System.Data.SqlTypes;
using System.Diagnostics;


namespace FilmekAdatbazis
{
	/// <summary>
	/// Summary description for Aru.
	/// </summary>
	public class felhasz : DAL
	{
		//		private System.Data.SqlClient.SqlCommand myUdateComm;
		private System.Data.SqlClient.SqlDataAdapter daGrid;
		private System.Data.SqlClient.SqlDataAdapter daAruk; 
		private System.Data.DataSet dsArukGrid;
		private System.Data.DataSet dsAruk;
		private System.Data.DataSet dsEgyAruSor;
		private System.Data.SqlClient.SqlDataAdapter daEgyAruSor;
		//private string ErrMess;
		private string sQuery;
		public felhasz(ref bool Err)
		{
			if (!base.IsConnectCreated())
			{
				Err = !base.MakeConnection();
			}
			daAruk = new SqlDataAdapter();
			daAruk.TableMappings.Add("Table", "Felhasznalok");
			daEgyAruSor = new SqlDataAdapter();
			daEgyAruSor.TableMappings.Add("Table", "EgyAru");
			//			dsArukGrid = new DataSet("ArukGrid");
			daGrid = new SqlDataAdapter();
			daGrid.TableMappings.Add("Table", "ArukGrid");
		}
	
		public System.Data.DataSet AruGridTolt(string sBetuk, ref string ErrM)
		{
			string sSQL = "SELECT FelhasznaloID, FelhasznaloNev, " +
				"Lakcim, Email, Telefonszam " + 
				" FROM Felhasznalok where FelhasznaloNev LIKE '" + sBetuk + "%'";
			dsArukGrid = base.myFillDataSetFromString(sSQL,"ArukGrid",ref daGrid,ref ErrM);
			return dsArukGrid;
			
		}
		
		public System.Data.DataSet ArukTolt(ref string ErrM)
		{
			string sSQL = "SELECT * FROM Felhasznalok";
			dsAruk = base.myFillDataSetFromString(sSQL,"Aruk",ref daAruk,ref ErrM);
			return dsAruk;
			
		}
		public System.Data.DataSet AruEgySorTolt(string sArukod, ref string ErrM)
		{
			String sSQL = "SELECT * FROM Felhasznalok WHERE FelhasznaloID="+sArukod;

			dsEgyAruSor = base.myFillDataSetFromString(sSQL,"EgyAru", ref daEgyAruSor,ref ErrM);
			return dsEgyAruSor;
			
									
		}
		public void EgySorModosit(System.Data.DataRow drEgySor,	ref string Err)
		{
			base.OpenConnection();
			daEgyAruSor.UpdateCommand = new SqlCommand("UPDATE Felhasznalok SET FelhasznaloId=@FelhasznaloId "+
               ", FelhasznaloNev = @FelhasznaloNev " +
				", Lakcim = @Lakcim , Email = @Email , Telefonszam = @Telefonszam "+
				"WHERE FelhasznaloID = @FelhasznaloID" ,base.theConnection());
			daEgyAruSor.UpdateCommand.Parameters.Add("@FelhasznaloNev", SqlDbType.VarChar, 40, "FelhasznaloNev");
			daEgyAruSor.UpdateCommand.Parameters.Add("@Lakcim", SqlDbType.VarChar, 40, "Lakcim");
            daEgyAruSor.UpdateCommand.Parameters.Add("@Email", SqlDbType.VarChar, 10, "Email");
            daEgyAruSor.UpdateCommand.Parameters.Add("@Telefonszam", SqlDbType.VarChar, 8, "Telefonszam");

			SqlParameter workParm = daEgyAruSor.UpdateCommand.Parameters.Add("@FelhasznaloID", SqlDbType.Int);
			workParm.SourceColumn = "FelhasznaloID";
			//workParm.SourceVersion = DataRowVersion.Original;
			try
			{
				daEgyAruSor.Update(dsEgyAruSor,"EgyAru");
				Err = "OK";
			}
			catch( DBConcurrencyException ex )
			{
				Err = ex.ToString();
				dsEgyAruSor.RejectChanges();
			}
							
			base.CloseConnection();
			
		}
		public System.Data.DataSet RefreshAruGrid(ref string Err)
		{
			base.OpenConnection();
			try
			{
				dsArukGrid.Clear();
				daGrid.Fill(dsArukGrid);				
				Err = "OK";
				return dsArukGrid;
			}
			catch( Exception ex )
			{
				Err = ex.ToString();
				return null;
			}
			finally
			{
				base.CloseConnection();
			}
		}
		public int MaxAruKod(ref string Err)
		{
			sQuery = "SELECT MAX(FelhasznaloID) FROM Felhasznalok";
			int iKod = base.myExecuteScalar(sQuery, ref Err);
			return iKod;
		}
		

				public void InsertParancs()
					//int iKod, string sNev, int iCsopKod, Double MennyRakt,
					//ref string Err)
				{
					
					daAruk.InsertCommand = new SqlCommand("INSERT INTO Felhasznalok VALUES (?, ?, ?, ?,?) ");
                    daAruk.InsertCommand.Parameters.Add("@FelhasznaloID", SqlDbType.Int, 8, "FelhasznaloID");
					daAruk.InsertCommand.Parameters.Add("@FelhasznaloNev", SqlDbType.VarChar, 40,"FelhasznaloNev");
                    daAruk.InsertCommand.Parameters.Add("@Lakcim", SqlDbType.VarChar, 8, "Lakcim");
                    daAruk.InsertCommand.Parameters.Add("@Email", SqlDbType.VarChar, 40, "Email");
                    daAruk.InsertCommand.Parameters.Add("@Telefonszam", SqlDbType.VarChar, 40, "Telefonszam");
                    daAruk.InsertCommand.Connection = base.theConnection();	
//					base.OpenConnection();
//					try
//					{
//						daAruk.Fill(dsAruk);
//						DataRow drAruSor = dsAruk.Tables["Aruk"].NewRow();
//						drAruSor.ItemArray = new object[] {iKod, sNev,  iCsopKod, MennyRakt};
//						dsAruk.Tables["Aruk"].Rows.Add(drAruSor);
//						daAruk.Update( dsAruk, "Aruk");
//						Err = "OK";
//					}
//					catch( Exception ex )
//					{
//						Err = ex.ToString();
//						dsEgyAruSor.RejectChanges();
//					}
//					base.CloseConnection();
				}
				
		public void UjSorIr(int iKod, string sNev, string cim, string mail,
              string tel,ref string Err)
		{
            sQuery = "INSERT INTO Felhasznalok VALUES (" +
Convert.ToString(iKod) + "," + " ' " + sNev + " ', " + " ' " + cim + " '," + " ' " + mail + " '," + " ' " + tel + "' ) ";
			base.myInsertCommand(sQuery,daAruk,ref Err);
			
		}
		public void TorolEgySort(int iKod, ref string Err)
		{
			sQuery = "DELETE FROM Felhasznalok WHERE FelhasznaloID = " +Convert.ToString(iKod);
			MyDeleteComm(sQuery,daEgyAruSor,ref Err);
		}
		public void WriteDataSetInDB(DataSet dsAruk, ref string Err)
		{
			InsertParancs();
			DataTable NewChildRecords = dsAruk.Tables["Felhasznalok"].GetChanges(DataRowState.Added);
			DataTable DeletedChildRecords = dsAruk.Tables["Felhasznalok"].GetChanges(DataRowState.Deleted);
			//workTable.PrimaryKey = new DataColumn[] {workTable.Columns["CustID"]};

			dsAruk.Tables["Aruk"].PrimaryKey= new DataColumn [] {dsAruk.Tables["Felhasznalok"].Columns["FelhasznaloID"]};
			try
			{
				if (DeletedChildRecords != null)
				{
					daAruk.Update(DeletedChildRecords);
					DeletedChildRecords.Dispose();
				}
				if (NewChildRecords != null)
				{
			//		daAruk.Update(NewChildRecords);
					daAruk.Update(dsAruk,"Aruk");
					NewChildRecords.Dispose();
				}
			}
			catch(Exception ex)
			{
				Err = ex.ToString();
				dsAruk.RejectChanges();
				
			}
		}
	}
}
